system-controller: disable LUA debug messages
[profile/ivi/murphy.git] / packaging.in / murphy.lua
1 with_system_controller = false
2 with_amb = false
3 verbose = 0
4
5 m = murphy.get()
6
7 -- try loading the various logging plugins
8 m:try_load_plugin('systemd')
9 m:try_load_plugin('dlog')
10
11 -- load the console plugin
12 m:try_load_plugin('console')
13
14 m:try_load_plugin('console.disabled', 'webconsole', {
15                   address = 'wsck:127.0.0.1:3000/murphy',
16                   httpdir = '/usr/share/murphy/webconsole' });
17
18 -- load the dbus plugin
19 if m:plugin_exists('dbus') then
20     m:load_plugin('dbus')
21 end
22
23 -- load the native resource plugin
24 if m:plugin_exists('resource-native') then
25     m:load_plugin('resource-native')
26     m:info("native resource plugin loaded")
27 else
28     m:info("No native resource plugin found...")
29 end
30
31 -- load the dbus resource plugin
32 m:try_load_plugin('resource-dbus', {
33     dbus_bus = "system",
34     dbus_service = "org.Murphy",
35     dbus_track = true,
36     default_zone = "driver",
37     default_class = "implicit"
38 })
39
40 -- load the domain control plugin
41 if m:plugin_exists('domain-control') then
42     m:load_plugin('domain-control')
43 else
44     m:info("No domain-control plugin found...")
45 end
46
47 if m:plugin_exists('glib') then
48     m:load_plugin('glib')
49 else
50     m:info("No glib plugin found...")
51 end
52
53 if m:plugin_exists("gam-resource-manager") then
54
55 function get_general_priorities(self)
56     print("*** get_general_priorities\n")
57     return { "USB Headset", "wiredHeadset", "speakers" }
58 end
59
60 function get_phone_priorities(self)
61     print("*** get_phone_priorities\n")
62     return { "wiredHeadset", "USB Headset" }
63 end
64
65 m:load_plugin('gam-resource-manager', {
66     config_dir = '/etc/murphy/gam',
67     decision_names = 'gam-wrtApplication-4',
68     max_active = 4,
69     app_mapping = {
70         ['t8j6HTRpuz.MediaPlayer'] = 'wrtApplication',
71         ['pacat'] = 'icoApplication'
72     },
73     app_default = 'icoApplication'
74 })
75
76 routing_sink_priority {
77     application_class = "player",
78     priority_queue = get_general_priorities
79 }
80
81 routing_sink_priority {
82     application_class = "game",
83     priority_queue = get_general_priorities
84 }
85
86 routing_sink_priority {
87     application_class = "implicit",
88     priority_queue = get_general_priorities
89 }
90
91 routing_sink_priority {
92     application_class = "phone",
93     priority_queue = get_phone_priorities
94 }
95
96 routing_sink_priority {
97     application_class = "basic",
98     priority_queue = get_phone_priorities
99 }
100
101 routing_sink_priority {
102     application_class = "event",
103     priority_queue = get_phone_priorities
104 }
105 end
106
107 -- load the AMB plugin
108 if m:plugin_exists('amb') then
109     m:try_load_plugin('amb')
110
111     if builtin.method.amb_initiate and
112        builtin.method.amb_update
113     then
114         with_amb = true
115     end
116 else
117     m:info("No amb plugin found...")
118 end
119
120 -- load the ASM resource plugin
121 if m:plugin_exists('resource-asm') then
122     m:try_load_plugin('resource-asm', {
123         zone = "driver",
124         share_mmplayer = "player:AVP,mandatory,exclusive,strict",
125         ignored_argv0 = "WebProcess"
126     })
127 else
128     m:info("No audio session manager plugin found...")
129 end
130
131 if m:plugin_exists('system-controller') then
132     with_system_controller = true
133 elseif m:plugin_exists('ivi-resource-manager') then
134     m:load_plugin('ivi-resource-manager')
135     with_system_controller = false
136 end
137
138 -- define application classes
139 application_class {
140     name     = "interrupt",
141     priority = 99,
142     modal    = true ,
143     share    = false,
144     order    = "fifo"
145 }
146
147 application_class {
148     name     = "emergency",
149     priority = 80,
150     modal    = false,
151     share    = false,
152     order    = "fifo"
153 }
154 application_class {
155     name     = "system",
156     priority = 52,
157     modal    = false,
158     share    = true,
159     order    = "lifo"
160 }
161 application_class {
162     name     = "alert",
163     priority = 51,
164     modal    = false,
165     share    = false,
166     order    = "fifo"
167 }
168
169 application_class {
170     name     = "navigator",
171     priority = 50,
172     modal    = false,
173     share    = true,
174     order    = "fifo"
175 }
176
177 application_class {
178     name     = "phone",
179     priority = 6 ,
180     modal    = false,
181     share    = true ,
182     order    = "lifo"
183 }
184 application_class {
185     name     = "camera",
186     priority = 5,
187     modal    = false,
188     share    = false,
189     order    = "lifo"
190 }
191
192 application_class { name="event"    , priority=4 , modal=false, share=true , order="fifo" }
193 application_class { name="game"     , priority=3 , modal=false, share=false, order="lifo" }
194 --# doesn't need to be created here, ivi-resource-manager creates it if loaded
195 --#application_class { name="basic"    , priority=2 , modal=false, share=false, order="lifo" }
196 application_class { name="player"   , priority=1 , modal=false, share=true , order="lifo" }
197 application_class { name="implicit" , priority=0 , modal=false, share=false, order="lifo" }
198
199 -- define zone attributes
200 zone.attributes {
201     type = {mdb.string, "common", "rw"},
202     location = {mdb.string, "anywhere", "rw"}
203 }
204
205 -- define zones
206 zone {
207      name = "driver",
208      attributes = {
209          type = "common",
210          location = "front-left"
211      }
212 }
213
214 zone {
215      name = "passanger1",
216      attributes = {
217          type = "private",
218          location = "front-right"
219      }
220 }
221
222 zone {
223      name = "passanger2",
224      attributes = {
225          type = "private",
226          location = "back-left"
227      }
228 }
229
230 zone {
231      name = "passanger3",
232      attributes = {
233          type = "private",
234          location = "back-right"
235      }
236 }
237
238 zone {
239      name = "passanger4",
240      attributes = {
241          type = "private",
242          location = "back-left"
243      }
244 }
245
246
247 -- define resource classes
248 if not m:plugin_exists('ivi-resource-manager') and
249    not with_system_controller and
250    not m:plugin_exists('gam-resource-manager')
251 then
252    resource.class {
253         name = "audio_playback",
254         shareable = true,
255         attributes = {
256             role = { mdb.string, "music", "rw" },
257             pid = { mdb.string, "<unknown>", "rw" },
258             policy = { mdb.string, "relaxed", "rw" },
259             source = { mdb.string, "webkit", "rw" },
260             conn_id = { mdb.unsigned, 0, "rw" }
261         }
262    }
263 end
264
265 if not m:plugin_exists('gam-resource-manager') then
266     resource.class {
267         name = "audio_recording",
268         shareable = true,
269         attributes = {
270              role   = { mdb.string, "music"    , "rw" },
271              pid    = { mdb.string, "<unknown>", "rw" },
272              policy = { mdb.string, "relaxed"  , "rw" }
273          }
274     }
275 end
276
277 resource.class {
278      name = "video_playback",
279      shareable = false,
280 }
281
282 resource.class {
283      name = "video_recording",
284      shareable = false
285 }
286
287 resource.class {
288      name = "speech_recognition",
289      shareable = true
290 }
291
292 resource.class {
293      name = "speech_synthesis",
294      shareable = true
295 }
296
297 -- PulseAudio volume context
298 mdb.table {
299     name = "volume_context",
300     index = { "id" },
301     create = true,
302     columns = {
303         { "id", mdb.unsigned },
304         { "value", mdb.string, 64 },
305     }
306 }
307
308 -- put default volume context to the table
309 mdb.table.volume_context:insert({ id = 1, value = "default" })
310
311 if not m:plugin_exists('ivi-resource-manager') and
312    not with_system_controller
313 then
314     resource.method.veto = {
315         function(zone, rset, grant, owners, req_set)
316             return true
317          end
318     }
319 end
320
321 -- test for creating selections
322 mdb.select {
323            name = "audio_owner",
324            table = "audio_playback_owner",
325            columns = {"application_class"},
326            condition = "zone_name = 'driver'"
327 }
328
329 mdb.select {
330            name = "vehicle_speed",
331            table = "amb_vehicle_speed",
332            columns = {"value"},
333            condition = "key = 'VehicleSpeed'"
334 }
335
336 element.lua {
337    name    = "speed2volume",
338    inputs  = { speed = mdb.select.vehicle_speed, param = 9 },
339    outputs = {  mdb.table { name = "speedvol",
340                             index = {"zone", "device"},
341                             columns = {{"zone", mdb.string, 16},
342                                        {"device", mdb.string, 16},
343                                        {"value", mdb.floating}},
344                             create = true
345                            }
346              },
347    oldvolume = 0.0,
348    update  = function(self)
349                 speed = self.inputs.speed.single_value
350                 if (speed) then
351                     volume = (speed - 144.0) / 7.0
352                 else
353                     volume = 0.0
354                 end
355                 diff = volume - self.oldvolume
356                 if (diff*diff > self.inputs.param) then
357                     print("*** element "..self.name.." update "..volume)
358                     self.oldvolume = volume
359                     mdb.table.speedvol:replace({zone = "driver", device = "speakers", value = volume})
360                 end
361              end
362 }
363
364 mdb.select {
365     name = "amb_state",
366     table = "amb_state",
367     columns = { "state" },
368     condition = "id = 0"
369 }
370
371 -- Night mode processing chain
372
373 mdb.select {
374     name = "exterior_brightness",
375     table = "amb_exterior_brightness",
376     columns = { "value" },
377     condition = "key = 'ExteriorBrightness'"
378 }
379
380 element.lua {
381     name    = "nightmode",
382     inputs  = { brightness = mdb.select.exterior_brightness },
383     oldmode = -1;
384     outputs = {
385         mdb.table {
386             name = "amb_nightmode",
387             index = { "id" },
388             create = true,
389             columns = {
390                 { "id", mdb.unsigned },
391                 { "night_mode", mdb.unsigned }
392             }
393         }
394     },
395     update = function(self)
396         -- This is a trivial function to calculate night mode. Later, we will
397         -- need a better threshold value and hysteresis to prevent oscillation.
398
399         brightness = self.inputs.brightness.single_value
400
401         if not brightness then
402             return
403         end
404
405         print("*** element "..self.name.." update brightness: "..brightness)
406
407         if brightness > 300 then
408             mode = 0
409         else
410             mode = 1
411         end
412
413         print("*** resulting mode: ".. mode)
414
415         if not (mode == self.oldmode) then
416             mdb.table.amb_nightmode:replace({ id = 0, night_mode = mode })
417         end
418
419         self.oldmode = mode
420     end
421 }
422
423 mdb.select {
424     name = "select_night_mode",
425     table = "amb_nightmode",
426     columns = { "night_mode" },
427     condition = "id = 0"
428 }
429
430 if with_amb then
431     sink.lua {
432         name = "night_mode",
433         inputs = { NightMode = mdb.select.select_night_mode,
434                    amb_state = mdb.select.amb_state },
435         property = "NightMode",
436         type = "b",
437         initiate = builtin.method.amb_initiate,
438         update = builtin.method.amb_update
439     }
440 end
441
442 -- Night mode general handlers
443
444 if with_system_controller then
445     sink.lua {
446         name = "nightmode_homescreen",
447         inputs = { owner = mdb.select.select_night_mode },
448         initiate = function(self)
449                 -- data = mdb.select.select_night_mode.single_value
450                 return true
451             end,
452         update = function(self)
453                 send_night_mode_to(homescreen)
454             end
455     }
456 end
457
458 -- Driving mode processing chain
459
460 element.lua {
461     name    = "drivingmode",
462     inputs  = { speed = mdb.select.vehicle_speed },
463     oldmode = -1;
464     outputs = {
465         mdb.table {
466             name = "amb_drivingmode",
467             index = { "id" },
468             create = true,
469             columns = {
470                 { "id", mdb.unsigned },
471                 { "driving_mode", mdb.unsigned }
472             }
473         }
474     },
475     update = function(self)
476
477         speed = self.inputs.speed.single_value
478
479         if not speed then
480             return
481         end
482
483         if speed == 0 then
484             mode = 0
485         else
486             mode = 1
487         end
488
489         if not (mode == self.oldmode) then
490             mdb.table.amb_drivingmode:replace({ id = 0, driving_mode = mode })
491         end
492
493         self.oldmode = mode
494     end
495 }
496
497 mdb.select {
498     name = "select_driving_mode",
499     table = "amb_drivingmode",
500     columns = { "driving_mode" },
501     condition = "id = 0"
502 }
503
504 if with_amb then
505     sink.lua {
506         name = "driving_mode",
507         inputs = { DrivingMode = mdb.select.select_driving_mode,
508                    amb_state = mdb.select.amb_state },
509         property = "DrivingMode",
510         type = "u",
511         initiate = builtin.method.amb_initiate,
512         update = builtin.method.amb_update
513     }
514 end
515
516 -- turn signals (left, right)
517
518 mdb.select {
519     name = "winker",
520     table = "amb_turn_signal",
521     columns = { "value" },
522     condition = "key = 'TurnSignal'"
523 }
524
525 -- define three categories
526
527 mdb.select {
528     name = "undefined_applications",
529     table = "aul_applications",
530     columns = { "appid" },
531     condition = "category = '<undefined>'"
532 }
533
534 mdb.select {
535     name = "basic_applications",
536     table = "aul_applications",
537     columns = { "appid" },
538     condition = "category = 'basic'"
539 }
540
541 mdb.select {
542     name = "entertainment_applications",
543     table = "aul_applications",
544     columns = { "appid" },
545     condition = "category = 'entertainment'"
546 }
547
548 function ft(t)
549     -- filter the object garbage out of the tables
550     ret = {}
551
552     for k,v in pairs(t) do
553         if k ~= "userdata" and k ~= "new" then
554             ret[k] = v
555         end
556     end
557
558     return ret
559 end
560
561 function getApplication(appid)
562     local conf = nil
563
564     -- find the correct local application definition
565
566     for k,v in pairs(ft(application)) do
567         if appid == v.appid then
568             conf = v
569             break
570         end
571     end
572
573     return conf
574 end
575
576 function regulateApplications(t, regulation)
577     for k,v in pairs(ft(t)) do
578
579         -- iterate through the undefined and entertainment apps, see if
580         -- they have been overruled in local config
581
582         local conf = getApplication(v.appid)
583
584         if conf and conf.resource_class ~= "player" then
585             -- override, don't disable
586             resmgr:disable_screen_by_appid("*", "*", v.appid, false, false)
587         else
588             resmgr:disable_screen_by_appid("*", "*", v.appid, regulation == 1, false)
589         end
590     end
591     resource.method.recalc("driver")
592 end
593
594 -- regulation (on), use "select_driving_mode"
595
596 sink.lua {
597     name = "driving_regulation",
598     inputs = { owner = mdb.select.select_driving_mode },
599     initiate = function(self)
600         -- local data = mdb.select.select_driving_mode.single_value
601         return true
602     end,
603     update = function(self)
604         local data = mdb.select.select_driving_mode.single_value
605
606         if verbose > 1 then
607             print("Driving mode updated: " .. tostring(data))
608         end
609
610         if not sc then
611             return true
612         end
613
614         -- tell homescreen that driving mode was updated
615         send_driving_mode_to(homescreen)
616
617         regulateApplications(ft(mdb.select.entertainment_applications), data)
618         regulateApplications(ft(mdb.select.undefined_applications), data)
619
620         return true
621     end
622 }
623 --[[
624 sink.lua {
625     name = "regulated_app_change",
626     inputs = { undef = mdb.select.undefined_applications,
627                entertainment = mdb.select.entertainment_applications },
628     initiate = function(self)
629         return true
630     end,
631     update = function(self)
632         local data = mdb.select.select_driving_mode.single_value
633
634         if not sc then
635             return
636         end
637
638         if verbose > 1 then
639             print("regulated application list was changed")
640         end
641
642         regulateApplications(ft(mdb.select.entertainment_applications), data)
643         regulateApplications(ft(mdb.select.undefined_applications), data)
644
645         return true
646     end
647 }
648 --]]
649
650 -- shift position (parking, reverse, other)
651
652 mdb.select {
653     name = "gear_position",
654     table = "amb_gear_position",
655     columns = { "value" },
656     condition = "key = 'GearPosition'"
657 }
658
659 -- cameras (back, front, left, right)
660
661 element.lua {
662     name    = "camera_state",
663     inputs  = { winker = mdb.select.winker, gear = mdb.select.gear_position },
664     oldmode = -1;
665     outputs = {
666         mdb.table {
667             name = "target_camera_state",
668             index = { "id" },
669             create = true,
670             columns = {
671                 { "id", mdb.unsigned },
672                 { "front_camera", mdb.unsigned },
673                 { "back_camera", mdb.unsigned },
674                 { "right_camera", mdb.unsigned },
675                 { "left_camera", mdb.unsigned }
676             }
677         }
678     },
679     update = function(self)
680
681         front_camera = 0
682         back_camera = 0
683         right_camera = 0
684         left_camera = 0
685
686         if self.inputs.gear == 128 then
687             back_camera = 1
688         elseif self.inputs.winker == 1 then
689             right_camera = 1
690         elseif self.inputs.winker == 2 then
691             left_camera = 1
692         end
693
694         mdb.table.target_camera_state:replace({ id = 0, front_camera = front_camera, back_camera = back_camera, right_camera = right_camera, left_camera = left_camera })
695
696     end
697 }
698
699 -- system controller test setup
700
701 if not with_system_controller then
702    -- ok, we should have 'audio_playback' defined by now
703    m:try_load_plugin('telephony')
704    return
705 end
706
707 m:load_plugin('system-controller')
708
709 window_manager_operation_names = {
710     [1] = "create",
711     [2] = "destroy"
712 }
713
714 function window_manager_operation_name(oper)
715     local name = window_manager_operation_names[oper]
716     if name then return name end
717     return "<unknown " .. tostring(oper) .. ">"
718 end
719
720 window_operation_names = {
721     [1] = "create",
722     [2] = "destroy",
723     [3] = "name_change",
724     [4] = "visible",
725     [5] = "configure",
726     [6] = "active",
727     [7] = "map",
728     [8] = "hint"
729 }
730
731 function window_operation_name(oper)
732     local name = window_operation_names[oper]
733     if name then return name end
734     return "<unknown " .. tostring(oper) .. ">"
735 end
736
737 layer_operation_names = {
738     [1] = "create",
739     [2] = "destroy",
740     [3] = "visible"
741 }
742
743 function layer_operation_name(oper)
744     local name = layer_operation_names[oper]
745     if name then return name end
746     return "<unknown " .. tostring(oper) .. ">"
747 end
748
749 input_manager_operation_names = {
750     [1] = "create",
751     [2] = "destroy",
752     [3] = "ready"
753 }
754
755 function input_manager_operation_name(oper)
756     local name = input_manager_operation_names[oper]
757     if name then return name end
758     return "<unknown " .. tostring(oper) .. ">"
759 end
760
761 input_operation_names = {
762     [1] = "create",
763     [2] = "destroy",
764     [3] = "update"
765 }
766
767 function input_operation_name(oper)
768     local name = input_operation_names[oper]
769     if name then return name end
770     return "<unknown " .. tostring(oper) .. ">"
771 end
772
773 code_operation_names = {
774     [1] = "create",
775     [2] = "destroy",
776     [3] = "state_change"
777 }
778
779 function code_operation_name(oper)
780     local name = code_operation_names[oper]
781     if name then return name end
782     return "<unknown " .. tostring(oper) .. ">"
783 end
784
785 command_names = {
786     [0x00001] = "send_appid",
787     [0x10001] = "create",
788     [0x10002] = "destroy",
789     [0x10003] = "show",
790     [0x10004] = "hide",
791     [0x10005] = "move",
792     [0x10006] = "animation",
793     [0x10007] = "change_active",
794     [0x10008] = "change_layer",
795     [0x10009] = "change_attr",
796     [0x10010] = "name",
797     [0x10020] = "map_thumb",
798     [0x10021] = "unmap_thumb",
799     [0x10022] = "map_get",
800     [0x10030] = "show layer",
801     [0x10031] = "hide_layer",
802     [0x10032] = "change_layer_attr",
803     [0x20001] = "add_input",
804     [0x20002] = "del_input",
805     [0x30001] = "change_user",
806     [0x30002] = "get_userlist",
807     [0x30003] = "get_lastinfo",
808     [0x30004] = "set_lastinfo",
809     [0x40001] = "acquire_res",
810     [0x40002] = "release_res",
811     [0x40003] = "deprive_res",
812     [0x40004] = "waiting_res",
813     [0x40005] = "revert_res",
814     [0x40006] = "window_id_res",
815     [0x40011] = "create_res",
816     [0x40012] = "destroy_res",
817     [0x50001] = "set_region",
818     [0x50002] = "unset_region",
819     [0x60001] = "change_state"
820 }
821
822 function command_name(command)
823     local name = command_names[command]
824     if name then return name end
825     return "<unknown " .. tostring(command) .. ">"
826 end
827
828 input_layer = {
829    [101] = true, -- input
830    [102] = true, -- touch
831    [103] = true  -- cursor
832 }
833
834 -- some day this should be merged with wmgr.layers
835 ico_layer_type = {
836    [1]   = 0x1000, -- background
837    [2]   = 0x2000, -- application
838    [3]   = 0x2000, -- homescreen
839    [4]   = 0x2000, -- interrupt application
840    [5]   = 0x2000, -- onscreen application
841    [6]   = 0xc000, -- startup
842    [7]   = 0x3000, -- fullscreen
843    [101] = 0x4000, -- input
844    [102] = 0xa000, -- touch
845    [103] = 0xb000  -- cursor
846 }
847
848 resmgr = resource_manager {
849   screen_event_handler = function(self, ev)
850                              local event = ev.event
851                              local surface = ev.surface
852
853                              if event == "init" then
854                                  if verbose > 0 then
855                                      print("*** init screen resource allocation -- disable all 'player'")
856                                  end
857                                  resmgr:disable_audio_by_appid("*", "player", "*", true, false)
858                              elseif event == "preallocate" then
859                                  if verbose > 0 then
860                                      print("*** preallocate screen resource "..
861                                            "for '" .. ev.appid .. "' -- enable 'player', if any")
862                                  end
863                                  resmgr:disable_audio_by_appid("*", "player", ev.appid, false, false)
864                              elseif event == "grant" then
865                                  if verbose > 0 then
866                                     print("*** make visible surface "..surface)
867                                  end
868                                  local a = animation({})
869                                  local r = m:JSON({surface = surface,
870                                                    visible = 1,
871                                                    raise   = 1})
872                                  wmgr:window_request(r,a,0)
873                              elseif event == "revoke" then
874                                  if verbose > 0 then
875                                     print("*** hide surface "..surface)
876                                  end
877                                  local a = animation({})
878                                  local r = m:JSON({surface = ev.surface,
879                                                    visible = 0})
880                                  wmgr:window_request(r,a,0)
881
882                              elseif event == "create" then
883
884                                 if verbose > 0 then
885                                     print("*** screen resource event: " ..
886                                           tostring(ev))
887                                 end
888
889                                 local regulation = mdb.select.select_driving_mode.single_value
890
891                                 if regulation == 1 then
892
893                                     local blacklisted = false
894
895                                     -- applications which have their category set to "entertainment"
896                                     -- or "undefined" are blacklisted, meaning they should be regulated
897
898                                     for i,v in pairs(ft(mdb.select.undefined_applications)) do
899                                         if v.appid == ev.appid then
900                                             if verbose > 0 then
901                                                 print(ev.appid .. " was blacklisted (undefined)")
902                                             end
903                                             blacklisted = true
904                                             break
905                                         end
906                                     end
907
908                                     if not blacklisted then
909                                         for i,v in pairs(ft(mdb.select.entertainment_applications)) do
910                                             if v.appid == ev.appid then
911                                                 if verbose > 0 then
912                                                     print(ev.appid .. " was blacklisted (entertainment)")
913                                                 end
914                                                 blacklisted = true
915                                                 break
916                                             end
917                                         end
918                                     end
919
920                                     -- our local application config, which takes precedence
921                                     local conf = getApplication(ev.appid)
922
923                                     -- disable only non-whitelisted applications
924                                     if not conf or conf.resource_class == "player" then
925                                         if blacklisted then
926                                             if verbose > 0 then
927                                                 print("disabling screen for " .. ev.appid)
928                                             end
929                                             resmgr:disable_screen_by_appid("*", "*", ev.appid, true, true)
930                                         end
931                                     end
932                                 end
933
934                              elseif event == "destroy" then
935                                if verbose > 0 then
936                                     print("*** screen resource event: " ..
937                                           tostring(ev))
938                                  end
939                              else
940                                  if verbose > 0 then
941                                     print("*** screen resource event: " ..
942                                           tostring(ev))
943                                  end
944                              end
945                          end,
946   audio_event_handler = function(self, ev)
947                              local event = ev.event
948                              local appid = ev.appid
949                              local audioid = ev.audioid
950
951                              if event == "grant" then
952                                  if verbose > 0 then
953                                     print("*** grant audio to "..appid..
954                                           " ("..audioid..") in '" ..
955                                           ev.zone .. "' zone")
956                                  end
957                              elseif event == "revoke" then
958                                  if verbose > 0 then
959                                     print("*** revoke audio from "..appid..
960                                           " ("..audioid..") in '" ..
961                                           ev.zone .. "' zone")
962                                  end
963                              else
964                                  if verbose > 0 then
965                                     print("*** audio resource event: " ..
966                                           tostring(ev))
967                                  end
968                              end
969                         end
970 }
971
972 resclnt = resource_client {}
973
974 wmgr = window_manager {
975   geometry = function(self, w,h, v)
976                   if type(v) == "function" then
977                       return v(w,h)
978                   end
979                   return v
980              end,
981
982   application = function(self, appid)
983                      if appid then
984                          local app = application_lookup(appid)
985                          if not app then
986                              app = application_lookup("default")
987                          end
988                          return app
989                      end
990                      return { privileges = {screen="none", audio="none"} }
991                 end,
992
993   output_order = { 1, 0 },
994
995   outputs = { { name  = "Mid",
996                 id    = 1,
997                 zone  = "driver",
998                 areas = { Full = {
999                               id     = 20,
1000                               pos_x  = 0,
1001                               pos_y  = 0,
1002                               width  = function(w,h) return w end,
1003                               height = function(w,h) return h end
1004                           },
1005                           Left = {
1006                               id     = 21,
1007                               pos_x  = 0,
1008                               pos_y  = 0,
1009                               width  = 320,
1010                               height = function(w,h) return h end
1011                           },
1012                           Right = {
1013                               id     = 22,
1014                               pos_x  = function(w,h) return w-320 end,
1015                               pos_y  = 0,
1016                               width  = 320,
1017                               height = function(w,h) return h end
1018                           }
1019                         }
1020                },
1021                { name  = "Center",
1022                  id    = 4,
1023                  zone  = "driver",
1024                  areas = { Status = {
1025                               id     = 0,
1026                               pos_x  = 0,
1027                               pos_y  = 0,
1028                               width  = function(w,h) return w end,
1029                               height = 64
1030                            },
1031                            Full = {
1032                               id     = 1,
1033                               pos_x  = 0,
1034                               pos_y  = 64,
1035                               width  = function(w,h) return w end,
1036                               height = function(w,h) return h-64-128 end
1037                            },
1038                            Upper = {
1039                               id     = 2,
1040                               pos_x  = 0,
1041                               pos_y  = 64,
1042                               width  = function(w,h) return w end,
1043                               height = function(w,h) return (h-64-128)/2 end
1044                            },
1045                            Lower = {
1046                               id     = 3,
1047                               pos_x  = 0,
1048                               pos_y  = function(w,h) return (h-64-128)/2+64 end,
1049                               width  = function(w,h) return w end,
1050                               height = function(w,h) return (h-64-128)/2 end
1051                            },
1052                            UpperLeft = {
1053                               id     = 4,
1054                               pos_x  = 0,
1055                               pos_y  = 64,
1056                               width  = function(w,h) return w/2 end,
1057                               height = function(w,h) return (h-64-128)/2 end
1058                            },
1059                            UpperRight = {
1060                               id     = 5,
1061                               pos_x  = function(w,h) return w/2 end,
1062                               pos_y  = 64,
1063                               width  = function(w,h) return w/2 end,
1064                               height = function(w,h) return (h-64-128)/2 end
1065                            },
1066                            LowerLeft = {
1067                               id     = 6,
1068                               pos_x  = 0,
1069                               pos_y  = function(w,h) return (h-64-128/2)+64 end,
1070                               width  = function(w,h) return w/2 end,
1071                               height = function(w,h) return (h-64-128)/2 end
1072                            },
1073                            LowerRight = {
1074                               id     = 7,
1075                               pos_x  = function(w,h) return w/2 end,
1076                               pos_y  = function(w,h) return (h-64-128/2)+64 end,
1077                               width  = function(w,h) return w/2 end,
1078                               height = function(w,h) return (h-64-128)/2 end
1079                            },
1080                            SysApp = {
1081                               id     = 8,
1082                               pos_x  = 0,
1083                               pos_y  = 64,
1084                               width  = function(w,h) return w end,
1085                               height = function(w,h) return h-64-128 end
1086                            },
1087                            ["SysApp.Left"] = {
1088                               id     = 9,
1089                               pos_x  = 0,
1090                               pos_y  = 64,
1091                               width  = function(w,h) return w/2-181 end,
1092                               height = function(w,h) return h-64-128 end
1093                            },
1094                            ["SysApp.Right"] = {
1095                               id     = 10,
1096                               pos_x  = function(w,h) return w/2+181 end,
1097                               pos_y  = 64,
1098                               width  = function(w,h) return w/2-181 end,
1099                               height = function(w,h) return h-64-128 end
1100                            },
1101                            MobileFull = {
1102                               id     = 11,
1103                               pos_x  = 0,
1104                               pos_y  = 64,
1105                               width  = function(w,h) return w end,
1106                               height = function(w,h) return h-64-128 end
1107                            },
1108                            MobileUpper = {
1109                               id     = 12,
1110                               pos_x  = 0,
1111                               pos_y  = 64,
1112                               width  = function(w,h) return w end,
1113                               height = function(w,h) return (h-64-128)/2 end
1114                            },
1115                            MobileLower = {
1116                               id     = 13,
1117                               pos_x  = 0,
1118                               pos_y  = function(w,h) return (h-64-128)/2+64 end,
1119                               width  = function(w,h) return w end,
1120                               height = function(w,h) return (h-64-128)/2 end
1121                            },                           
1122                            Control = {
1123                               id     = 14,
1124                               pos_x  = 0,
1125                               pos_y  = function(w,h) return h-128 end,
1126                               width  = function(w,h) return w end,
1127                               height = 128
1128                            },
1129                         }
1130               }
1131   },
1132              --    id   name            type  output
1133   layers = { {      0, "BackGround"   ,    1, "Center" },
1134              {      1, "Application"  ,    2, "Center" },
1135              {      2, "HomeScreen"   ,    3, "Center" },
1136              {      3, "ControlBar"   ,    3, "Center" },
1137              {      4, "InterruptApp" ,    4, "Center" },
1138              {      5, "OnScreen"     ,    5, "Center" },
1139              {      6, "Touch"        ,  102, "Center" },
1140              {      7, "Cursor"       ,  103, "Center" }
1141   },
1142
1143
1144   manager_update = function(self, oper)
1145                        if verbose > 0 then
1146                            print("### <== WINDOW MANAGER UPDATE:" ..
1147                                  window_manager_operation_name(oper))
1148                        end
1149                        if oper == 1 then
1150                            local wumask = window_mask { --raise   = true,
1151                                                         visible = true,
1152                                                         active  = true }
1153                            local wrmask = window_mask { raise   = true,
1154                                                         active  = true,
1155                                                         layer   = true }
1156                            local lumask = layer_mask  { visible = true }
1157                            local lrmask = layer_mask  { visible = true }
1158                            local req = m:JSON({
1159                                passthrough_window_update  = wumask:tointeger(),
1160                                passthrough_window_request = wrmask:tointeger(),
1161                                passthrough_layer_update   = lumask:tointeger(),
1162                                passthrough_layer_request  = lrmask:tointeger()
1163                            })
1164                            self:manager_request(req)
1165                        end
1166                    end,
1167
1168   window_update = function(self, oper, win, mask)
1169                       if verbose > 0 then
1170                           print("### <== WINDOW UPDATE oper:" ..
1171                                 window_operation_name(oper) ..
1172                                 " mask: " .. tostring(mask))
1173                           if verbose > 1 then
1174                               print(win)
1175                           end
1176                       end
1177
1178                       local arg = m:JSON({ surface = win.surface,
1179                                            winname = win.name
1180                       })
1181                       local command = 0
1182
1183                       if oper == 1 then -- create
1184                            local layertype = win.layertype
1185                            if layertype and input_layer[layertype] then
1186                                if verbose > 0 then
1187                                    print("ignoring input panel creation")
1188                                end
1189                                return
1190                            end
1191                            command     = 0x10001
1192                       elseif oper == 2 then -- destroy
1193                            command     = 0x10002
1194                       elseif oper == 3 then  -- namechange
1195                            command     = 0x10010
1196                       elseif oper == 4 or oper == 5 then -- visible/configure
1197                            command     = 0x10009
1198                            arg.zone    = win.area
1199                            arg.node    = win.node
1200                            if win.layertype then
1201                                arg.layertype = win.layertype
1202                            end
1203                            arg.layer   = win.layer
1204                            arg.pos_x   = win.pos_x
1205                            arg.pos_y   = win.pos_y
1206                            arg.width   = win.width
1207                            arg.height  = win.height
1208                            arg.raise   = win.raise
1209                            arg.visible = win.visible
1210                            if win.active == 0 then
1211                                arg.active = 0
1212                            else
1213                                arg.active = 1
1214                            end
1215                       elseif oper == 6 then -- active
1216                            if win.active == 0 then
1217                                if verbose > 0 then
1218                                    print("ignoring inactive event")
1219                                end
1220                                return
1221                            end
1222                            command = 0x10007
1223                       elseif oper == 7 then -- map
1224                            local map = win.map
1225                            if not map then
1226                                return
1227                            end
1228                            if win.mapped == 0 then
1229                                command = 0x10021
1230                            else
1231                                command = 0x10020
1232                            end
1233                            arg.attr = map.type
1234                            --arg.name = map.target
1235                            arg.width = map.width
1236                            arg.height = map.height
1237                            arg.stride = map.stride
1238                            arg.format = map.format
1239                       else
1240                            if verbose > 0 then
1241                                print("### nothing to do")
1242                            end
1243                            return
1244                       end
1245
1246                       local msg = m:JSON({ command = command,
1247                                            appid   = win.appid,
1248                                            pid     = win.pid,
1249                                            arg     = arg
1250                       })
1251                       if verbose > 0 then
1252                           print("### <== sending " ..
1253                                 command_name(msg.command) ..
1254                                 " window message to '" .. homescreen .. "'")
1255                           if verbose > 1 then
1256                               print(msg)
1257                           end
1258                       end
1259                       sc:send_message(homescreen, msg)
1260
1261                       if oper == 1 then -- create
1262                           local i = input_layer[win.layertype]
1263                           local p = self:application(win.appid)
1264                           local s = p.privileges.screen
1265
1266                           if s == "system" then
1267                               local a = animation({})
1268                               local r = m:JSON({surface = win.surface,
1269                                                 visible = 1,
1270                                                 raise   = 1})
1271                               self:window_request(r,a,0)
1272                           else
1273                               if i then
1274                                   if verbose > 0 then
1275                                       print("do not make resource for " ..
1276                                             "input window")
1277                                   end
1278                               else
1279                                   resclnt:resource_set_create("screen",
1280                                                                "driver",
1281                                                                win.appid,
1282                                                                win.surface)
1283                                   special_screen_sets[win.surface] = true
1284                               end
1285                           end
1286
1287                           if onscreen and win.appid == onscreen then
1288                               local resmsg = m:JSON({
1289                                         command = 0x40006, -- window_id_res
1290                                         appid   = win.appid,
1291                                         pid     = win.pid,
1292                                         res     = m:JSON({
1293                                             window  = m:JSON({
1294                                                 ECU     = "",
1295                                                 display = "",
1296                                                 layer   = "",
1297                                                 layout  = "",
1298                                                 area    = "",
1299                                                 dispatchApp = "",
1300                                                 role        = win.name,
1301                                                 resourceId  = win.surface
1302                                             })
1303                                         })
1304                               })
1305                               if verbose > 0 then
1306                                   print("### <== sending " ..
1307                                         command_name(resmsg.command) ..
1308                                         " message to '" .. onscreen .. "'")
1309                                   if verbose > 1 then
1310                                       print(resmsg)
1311                                   end
1312                               end
1313                               sc:send_message(onscreen, resmsg);
1314                           end
1315                       elseif oper == 2 then -- destroy
1316                           resclnt:resource_set_destroy("screen", win.surface)
1317                           special_screen_sets[win.surface] = nil
1318                       elseif oper == 6 then -- active
1319                           if win.active then
1320                               local i = input_layer[win.layertype]
1321                               local p = self:application(win.appid)
1322                               local s = p.privileges.screen
1323                               local surface = win.surface
1324                               if not i and s ~= "system" then
1325                                  resclnt:resource_set_acquire("screen",surface)
1326                                  resmgr:window_raise(win.appid, surface, 1)
1327                               end
1328                           end
1329                       end
1330                   end,
1331
1332   layer_update = function(self, oper, layer, mask)
1333                       if verbose > 0 then
1334                           print("### LAYER UPDATE:" ..
1335                                 layer_operation_name(oper) ..
1336                                 " mask: " .. tostring(mask))
1337                           if verbose > 1 then
1338                               print(layer)
1339                           end
1340                       end
1341                       if oper == 3 then -- visible
1342                          local command = 0x10008 -- change_layer
1343                          local msg = m:JSON({
1344                                          command = command,
1345                                          appid = "",
1346                                          arg = m:JSON({layer = layer.id,
1347                                                        visible = layer.visible
1348                                          })
1349                                 })
1350                          if verbose > 0 then
1351                             print("### <== sending "..command_name(command)..
1352                                   " layer message")
1353                             if verbose > 1 then
1354                                 print(msg)
1355                             end
1356                          end
1357                          sc:send_message(homescreen, msg)
1358                       else
1359                            if verbose > 0 then
1360                                print("### nothing to do")
1361                            end
1362                       end
1363                  end,
1364
1365   output_update = function(self, oper, out, mask)
1366                       local idx = out.index
1367                       local defidx = self.output_order[idx+1]
1368                       if verbose > 0 then
1369                           print("### OUTPUT UPDATE:" .. oper ..
1370                                 " mask: "..tostring(mask))
1371                       end
1372                       if not defidx then
1373                           return
1374                       end
1375                       print(out)
1376                       local outdef = self.outputs[defidx+1]
1377                       if (oper == 1) then -- create
1378                           if outdef then
1379                               self:output_request(m:JSON({index = idx,
1380                                                           id    = outdef.id,
1381                                                           name  = outdef.name
1382                                                           }))
1383                           end
1384                       elseif (oper == 5) then -- done
1385                           local ads = outdef.areas
1386                           local on = outdef.name
1387                           if ads then
1388                               for name,ad in pairs(ads) do
1389                                   local can = wmgr:canonical_name(on.."."..name)
1390                                   local a = m:JSON({name   = name,
1391                                                     output = out.index})
1392                                   for fld,val in pairs(ad) do
1393                                       a[fld] = self:geometry(out.width,
1394                                                              out.height,
1395                                                              val)
1396                                    end
1397                                    self:area_create(a)
1398                                    resmgr:area_create(area[can], outdef.zone)
1399                               end
1400                           end
1401                       end
1402                   end
1403 }
1404
1405
1406 imgr = input_manager {
1407   inputs = {{ name = "G27 Racing Wheel",
1408               id = 0,
1409               switch = { [2] = {appid="org.tizen.ico.app-soundsample"      },
1410                          [3] = {appid="org.tizen.ico.homescreen", keycode=1},
1411                          [4] = {appid="org.tizen.ico.app-soundsample"      },
1412                          [5] = {appid="org.tizen.ico.homescreen", keycode=2}
1413              }}
1414   },
1415
1416   manager_update = function(self, oper)
1417                        if verbose > 0 then
1418                            print("### <== INPUT MANAGER UPDATE:" ..
1419                                  input_manager_operation_name(oper))
1420                        end
1421                    end,
1422
1423   input_update = function(self, oper, inp, mask)
1424                      if verbose > 0 then
1425                          print("### INPUT UPDATE:" ..
1426                                 input_operation_name(oper) ..
1427                                 " mask: " .. tostring(mask))
1428                           if verbose > 1 then
1429                               print(inp)
1430                           end
1431                       end
1432                  end,
1433   code_update  = function(self, oper, code, mask)
1434                      if verbose > 0 then
1435                          print("### CODE UPDATE: mask: " .. tostring(mask))
1436                          if verbose > 1 then
1437                              print(code)
1438                          end
1439                      end
1440                      local msg = m:JSON({ command = 1,
1441                                           appid = "org.tizen.ico.homescreen",
1442                                           arg = m:JSON({ device = code.device,
1443                                                          time = code.time,
1444                                                          input = code.input,
1445                                                          code = code.id,
1446                                                          state = code.state
1447                                            })
1448                      })
1449                      if verbose > 0 then
1450                          print("### <== sending " ..
1451                                command_name(msg.command) ..
1452                                " input message")
1453                          if verbose > 1 then
1454                              print(msg)
1455                          end
1456                      end
1457                      sc:send_message(homescreen, msg)
1458                  end
1459 }
1460
1461 sc = m:get_system_controller()
1462
1463 -- resource sets
1464 sets = {}
1465
1466 -- special screen resource sets
1467 -- TODO: just rewrite screen resource handling to use regular resource API
1468
1469 special_screen_sets = {}
1470
1471 -- user manager
1472 um = m:UserManager()
1473
1474 connected = false
1475 homescreen = ""
1476 onscreen = ""
1477
1478 -- these shoud be before wmgr:connect() is called
1479 if verbose > 0 then
1480    print("====== creating applications ======")
1481 end
1482 application {
1483     appid          = "default",
1484     area           = "Center.Full",
1485     privileges     = { screen = "none", audio = "none" },
1486     resource_class = "player",
1487     screen_priority = 0
1488 }
1489
1490 application {
1491     appid           = "weston",
1492     area            = "Center.Full",
1493     privileges      = { screen = "system", audio = "none" },
1494     resource_class  = "implicit",
1495     screen_priority = 30
1496 }
1497
1498 application {
1499     appid           = "org.tizen.ico.homescreen",
1500     area            = "Center.Full",
1501     windows         = { {'ico_hs_controlbarwindow', 'Center.Control'} },
1502     privileges      = { screen = "system", audio = "system" },
1503     resource_class  = "player",
1504     screen_priority = 20
1505 }
1506
1507 application {
1508     appid           = "org.tizen.ico.statusbar",
1509     area            = "Center.Status",
1510     privileges      = { screen = "system", audio = "none" },
1511     resource_class  = "player",
1512     screen_priority = 20
1513 }
1514
1515 application {
1516     appid           = "org.tizen.ico.onscreen",
1517     area            = "Center.Full",
1518     privileges      = { screen = "system", audio = "system" },
1519     resource_class  = "player",
1520     screen_priority = 20
1521 }
1522
1523 application {
1524     appid           = "org.tizen.ico.login",
1525     area            = "Center.Full",
1526     privileges      = { screen = "system", audio = "system" },
1527     resource_class  = "player",
1528     screen_priority = 20
1529 }
1530
1531 application {
1532     appid           = "org.tizen.ico.camera_left",
1533     area            = "Center.SysApp.Left",
1534     privileges      = { screen = "system", audio = "none" },
1535     requisites      = { screen = "blinker_left", audio = "none" },
1536     resource_class  = "player",
1537     screen_priority = 30
1538 }
1539
1540 application {
1541     appid           = "org.tizen.ico.camera_right",
1542     area            = "Center.SysApp.Right",
1543     privileges      = { screen = "system", audio = "none" },
1544     requisites      = { screen = "blinker_right", audio = "none" },
1545     resource_class  = "player",
1546     screen_priority = 30
1547 }
1548
1549 application {
1550     appid           = "net.zmap.navi",
1551     area            = "Center.Full",
1552     privileges      = { screen = "none", audio = "none" },
1553     resource_class  = "navigator",
1554     screen_priority = 30
1555 }
1556
1557 application {
1558     appid           = "GV3ySIINq7.GhostCluster",
1559     area            = "Center.Full",
1560     privileges      = { screen = "none", audio = "none" },
1561     resource_class  = "system",
1562     screen_priority = 30
1563 }
1564
1565 if sc then
1566     sc.client_handler = function (self, cid, msg)
1567         local command = msg.command
1568         local appid = msg.appid
1569         if verbose > 0 then
1570             print('### ==> client handler:')
1571             if verbose > 1 then
1572                 print(msg)
1573             end
1574         end
1575
1576         -- known commands: 1 for SEND_APPID, synthetic command 0xFFFF for
1577         -- disconnection
1578
1579         if command == 0xFFFF then
1580             if verbose > 1 then
1581                 print('client ' .. cid .. ' disconnected')
1582             end
1583             if msg.appid == homescreen then
1584                 homescreen = ""
1585                 for i,v in pairs(special_screen_sets) do
1586                     resclnt:resource_set_destroy("screen", i)
1587                     special_screen_sets[i] = nil
1588                 end
1589             end
1590             return
1591         end
1592
1593         -- handle the connection to weston
1594
1595         if appid then
1596             if appid == "org.tizen.ico.homescreen" then
1597                 print('Setting homescreen='..appid)
1598                 homescreen = appid
1599                 if command and command == 1 then
1600                     send_driving_mode_to(homescreen)
1601                     send_night_mode_to(homescreen)
1602                 end
1603             elseif appid == "org.tizen.ico.onscreen" then
1604                 onscreen = appid
1605                 if command and command == 1 then
1606                     send_driving_mode_to(onscreen)
1607                     send_night_mode_to(onscreen)
1608                 end
1609             end
1610
1611             if not connected and appid == "org.tizen.ico.homescreen" then
1612                 print('Trying to connect to weston...')
1613                 connected = wmgr:connect()
1614             end
1615         end
1616     end
1617
1618     sc.generic_handler = function (self, cid, msg)
1619         if verbose > 0 then
1620             print('### ==> generic handler:')
1621             if verbose > 1 then
1622                print(msg)
1623             end
1624         end
1625     end
1626
1627     sc.window_handler = function (self, cid, msg)
1628         if verbose > 0 then
1629             print('### ==> received ' ..
1630                    command_name(msg.command) .. ' message')
1631             if verbose > 1 then
1632                 print(tostring(msg))
1633             end
1634         end
1635
1636         local a = animation({})
1637         local nores = false
1638         if msg.command == 0x10003 then       -- ico SHOW command
1639             local raise_mask = 0x01000000
1640             local lower_mask = 0x02000000
1641             local nores_mask = 0x40000000
1642             local time_mask  = 0x00ffffff
1643             msg.arg.visible = 1
1644             if msg.arg then
1645                 local time = 200
1646                 if  msg.arg.anim_time then
1647                     local t = msg.arg.anim_time
1648                     time = m:AND(t, time_mask)
1649                     nores = not m:AND(t, nores_mask)
1650                     if m:AND(t, raise_mask) then
1651                         msg.arg.raise = 1
1652                     elseif m:AND(t, lower_mask) then
1653                         msg.arg.raise = 0
1654                     end
1655                 end
1656                 if msg.arg.anim_name then
1657                     a.show = { msg.arg.anim_name, time }
1658                     print('time: ' .. tostring(a.show[2]))
1659                 end
1660             end
1661             if not nores then
1662                 local p = wmgr:application(msg.appid)
1663                 local s = p.privileges.screen
1664                 if s == "system" then
1665                     nores = true
1666                     if not msg.arg.raise then
1667                         msg.arg.raise = 1
1668                     end
1669                 end
1670             end
1671             if verbose > 2 then
1672                 print('### ==> SHOW')
1673                 print(tostring(msg.arg))
1674             end
1675             if nores then
1676                 wmgr:window_request(msg.arg, a, 0)
1677             else
1678                 local surface = msg.arg.surface
1679                 resclnt:resource_set_acquire("screen", surface)
1680                 resmgr:window_raise(msg.appid, surface, 1)
1681             end
1682         elseif msg.command == 0x10004 then   -- ico HIDE command
1683             local raise_mask = 0x01000000
1684             local lower_mask = 0x02000000
1685             local nores_mask = 0x40000000
1686             local time_mask  = 0x00ffffff
1687             msg.arg.visible = 0
1688             if msg.arg then
1689                 local time = 200
1690                 if msg.arg.anim_time then
1691                     local t = msg.arg.anim_time
1692                     time = m:AND(t, time_mask)
1693                     nores = not m:AND(t, nores_mask)
1694                 end
1695                 if msg.arg.anim_name then
1696                     a.hide = { msg.arg.anim_name, time }
1697                     print('hide animation time: ' .. tostring(a.hide[2]))
1698                 end
1699             end
1700             if not nores then
1701                 local p = wmgr:application(msg.appid)
1702                 local s = p.privileges.screen
1703                 if s == "system" then
1704                     nores = true
1705                     msg.arg.raise = 0
1706                 end
1707             end
1708             if verbose > 2 then
1709                 print('### ==> HIDE REQUEST')
1710                 print(tostring(msg.arg))
1711             end
1712             if nores then
1713                 wmgr:window_request(msg.arg, a, 0)
1714             else
1715                 resmgr:window_raise(msg.appid, msg.arg.surface, -1)
1716             end
1717         elseif msg.command == 0x10005 then   -- ico MOVE
1718             if verbose > 2 then
1719                 print('### ==> MOVE REQUEST')
1720                 print(tostring(msg.arg))
1721             end
1722             if msg.arg.zone then
1723                 msg.arg.area = msg.arg.zone
1724             end
1725             wmgr:window_request(msg.arg, a, 0)
1726             -- TODO: handle if area changed
1727         elseif msg.command == 0x10007 then   -- ico CHANGE_ACTIVE
1728             if not msg.arg.active then
1729                 msg.arg.active = 3 -- pointer + keyboard
1730             end
1731             if verbose > 2 then
1732                 print('### ==> CHANGE_ACTIVE REQUEST')
1733                 print(tostring(msg.arg))
1734             end
1735             wmgr:window_request(msg.arg, a, 0)
1736         elseif msg.command == 0x10008 then   -- ico CHANGE_LAYER
1737             if verbose > 2 then
1738                 print('### ==> CHANGE_LAYER REQUEST')
1739                 print(tostring(msg.arg))
1740             end
1741             --[[
1742             if msg.arg.layer ~= 4 or msg.arg.layer ~= 5 then
1743                 print("do not change layer for other than cursor or touch")
1744                 return
1745             end
1746             --]]
1747             wmgr:window_request(msg.arg, a, 0)
1748         elseif msg.command == 0x10020 then   -- ico MAP_THUMB
1749             local framerate = msg.arg.framerate
1750             local animname  = msg.arg.anim_name
1751             if animname then
1752                a.map = { animname, 1 }
1753                if not framerate or framerate < 0 then
1754                   framerate = 5
1755                end
1756                msg.arg.mapped = 1
1757                if verbose > 2 then
1758                   print('### ==> MAP_THUMB REQUEST')
1759                   print(msg.arg)
1760                   print('framerate: '..framerate)
1761                end
1762                wmgr:window_request(msg.arg, a, framerate)
1763             end
1764         elseif msg.command == 0x10021 then   -- ico UNMAP_THUMB
1765             msg.arg.mapped = 0
1766             if verbose > 2 then
1767                 print('### ==> UNMAP_THUMB REQUEST')
1768                 print(msg.arg)
1769             end
1770             wmgr:window_request(msg.arg, a, 0)
1771 --[[
1772         elseif msg.command == 0x10013 then -- ico MAP_BUFFER command
1773             local shmname = msg.arg.anim_name
1774             local bufsize = msg.arg.width
1775             local bufnum  = msg.arg.height
1776             if shmname and bufsize and bufnum then
1777                 if verbose > 2 then
1778                     print('### ==> MAP_BUFFER REQUEST')
1779                     print("shmaname='" .. shmname ..
1780                           "' bufsize='" .. bufsize ..
1781                           " bufnum=" .. bufnum)
1782                 end
1783                 wmgr:buffer_request(shmname, bufsize, bufnum)
1784             end
1785 --]]
1786         elseif msg.command == 0x10030 then   -- ico SHOW_LAYER command
1787             msg.arg.visible = 1
1788             if verbose > 2 then
1789                 print('### ==> SHOW_LAYER REQUEST')
1790                 print(msg.arg)
1791             end
1792             wmgr:layer_request(msg.arg)
1793         elseif msg.command == 0x10031 then   -- ico HIDE_LAYER command
1794             msg.arg.visible = 0
1795             if verbose > 2 then
1796                 print('### ==> HIDE_LAYER REQUEST')
1797                 print(msg.arg)
1798             end
1799             wmgr:layer_request(msg.arg)
1800         end
1801     end
1802
1803     sc.input_handler = function (self, cid, msg)
1804         if verbose > 0 then
1805             print('### ==> input handler: ' .. command_name(msg.command))
1806             if verbose > 1 then
1807                 print(msg)
1808             end
1809         end
1810         if msg.command == 0x20001 then -- add_input
1811             msg.arg.appid = msg.appid
1812             if verbose > 2 then
1813                 print('### ==> ADD_INPUT REQUEST')
1814                 print(tostring(msg.arg))
1815             end
1816             imgr:input_request(msg.arg)
1817         elseif msg.command == 0x20002 then -- del_input
1818             msg.arg.appid = ''
1819             if verbose > 2 then
1820                 print('### ==> DEL_INPUT REQUEST')
1821                 print(tostring(msg.arg))
1822             end
1823             imgr:input_request(msg.arg)
1824         -- elseif msg.command == 0x20003 then -- send_input
1825         end
1826     end
1827
1828     sc.user_handler = function (self, cid, msg)
1829         if verbose > 0 then
1830             print('### ==> user handler: ' .. command_name(msg.command))
1831             if verbose > 1 then
1832                 print(msg)
1833             end
1834         end
1835
1836         if not um then
1837             print("User Manager not initialized")
1838             return
1839         end
1840
1841         if msg.command == 0x30001 then -- change_user
1842             print("command CHANGE_USER")
1843             if not msg.arg then
1844                 print("invalid message")
1845                 return
1846             end
1847
1848             username = msg.arg.user
1849             passwd = msg.arg.pass
1850
1851             if not username then
1852                 username = ""
1853             end
1854
1855             if not passwd then
1856                 passwd = ""
1857             end
1858
1859             success = um:changeUser(username, passwd)
1860
1861             if not success then
1862                 reply = m.JSON({
1863                     appid = msg.appid,
1864                     command = cmd
1865                 })
1866                 if sc:send_message(msg.appid, reply) then
1867                     print('*** sent authentication failed message')
1868                 else
1869                     print('*** failed to send authentication failed message')
1870                 end
1871             end
1872
1873         elseif msg.command == 0x30002 then -- get_userlist
1874             print("command GET_USERLIST")
1875             if not msg.appid then
1876                 print("invalid message")
1877                 return
1878             end
1879
1880             users, currentUser = um:getUserList()
1881
1882             if not users then
1883                 print("failed to get user list")
1884                 return
1885             end
1886
1887             nUsers = 0
1888
1889             for i,v in pairs(users) do
1890                 nUsers = nUsers + 1
1891             end
1892
1893             if not currentUser then
1894                 currentUser = ""
1895             end
1896
1897             if verbose > 1 then
1898                 print("current user: " .. currentUser)
1899                 print("user list:")
1900                 for i,v in pairs(users) do
1901                     print(v)
1902                 end
1903             end
1904
1905             reply = m.JSON({
1906                 appid = msg.appid,
1907                 command = cmd,
1908                 arg = m.JSON({
1909                     user_num = nUsers,
1910                     user_list = users,
1911                     user_login = currentUser
1912                 })
1913             })
1914
1915             if verbose > 1 then
1916                 print("### <== GetUserList reply: " .. tostring(reply))
1917             end
1918
1919             if sc:send_message(msg.appid, reply) then
1920                 print('*** reply OK')
1921             else
1922                 print('*** reply FAILED')
1923             end
1924
1925         elseif msg.command == 0x30003 then -- get_lastinfo
1926             print("command GET_LASTINFO")
1927             if not msg.appid then
1928                 print("invalid message")
1929                 return
1930             end
1931
1932             lastInfo = um:getLastInfo(msg.appid)
1933
1934             if not lastInfo then
1935                 print("failed to get last info for app" .. msg.appid)
1936                 return
1937             end
1938
1939             reply = m.JSON({
1940                 appid = msg.appid,
1941                 command = cmd,
1942                 arg = m.JSON({
1943                     lastinfo = lastinfo
1944                 })
1945             })
1946
1947             if sc:send_message(msg.appid, reply) then
1948                 print('*** reply OK')
1949             else
1950                 print('*** reply FAILED')
1951             end
1952
1953         elseif msg.command == 0x30004 then -- set_lastinfo
1954             print("command SET_LASTINFO")
1955             if not msg.arg or not msg.appid then
1956                 print("invalid message")
1957                 return
1958             end
1959
1960             lastInfo = um:setLastInfo(msg.appid, msg.arg.lastinfo)
1961         end
1962     end
1963
1964     sc.resource_handler = function (self, cid, msg)
1965         if verbose > 0 then
1966             print('### ==> resource handler: ' .. command_name(msg.command))
1967             if verbose > 1 then
1968                 print(msg)
1969             end
1970         end
1971
1972         createResourceSet = function (ctl, client, msg)
1973             cb = function(rset, data)
1974                 print("> resource callback")
1975
1976                 -- type is either basic (0) or interrupt (1)
1977                 requestType = 0
1978                 if msg.res.type then
1979                     requestType = msg.res.type
1980                 end
1981
1982                 if rset.acquired then
1983                     cmd = 0x00040001 -- acquire
1984                 else
1985                     cmd = 0x00040002 -- release
1986                 end
1987
1988                 reply = m.JSON({
1989                         appid = data.client,
1990                         command = cmd,
1991                         res = {
1992                             type = requestType
1993                         }
1994                     })
1995
1996                 if rset.resources.audio_playback then
1997                     reply.res.sound = {
1998                         zone = "driver",
1999                         name = msg.appid,
2000                         adjust = 0,
2001                         -- id = "0"
2002                     }
2003                 end
2004
2005                 if rset.resources.display then
2006                     reply.res.window = {
2007                         zone = "driver",
2008                         name = msg.appid,
2009                         -- id = "0"
2010                     }
2011                 end
2012
2013                 if rset.resources.input then
2014                     reply.res.input = {
2015                         name = msg.appid,
2016                         event = 0
2017                     }
2018                 end
2019                 print("sending message to client: " .. data.client)
2020
2021                 if sc:send_message(data.client, reply) then
2022                     print('*** reply OK')
2023                 else
2024                     print('*** reply FAILED')
2025                 end
2026             end
2027
2028             rset = m:ResourceSet({
2029                     application_class = "player",
2030                     zone = "driver", -- msg.zone ("full")
2031                     callback = cb
2032                 })
2033
2034             rset.data = {
2035                 cid = cid,
2036                 ctl = ctl
2037             }
2038
2039             if msg.res.sound then
2040                 rset:addResource({
2041                         resource_name = "audio_playback"
2042                     })
2043                 rset.resources.audio_playback.attributes.pid = tostring(msg.pid)
2044                 rset.resources.audio_playback.attributes.appid = msg.appid
2045                 print("sound name: " .. msg.res.sound.name)
2046                 print("sound zone:" .. msg.res.sound.zone)
2047                 print("sound adjust: " .. tostring(msg.res.sound.adjust))
2048                 if msg.res.sound.id then
2049                     print("sound id: " .. msg.res.sound.id)
2050                 end
2051             end
2052
2053             if msg.res.input then
2054                 rset:addResource({
2055                         resource_name = "input"
2056                     })
2057                 rset.resources.input.attributes.pid = tostring(msg.pid)
2058                 rset.resources.input.attributes.appid = msg.appid
2059                 print("input name: " .. msg.res.sound.name)
2060                 print("input event:" .. tostring(msg.res.input.event))
2061             end
2062
2063             if msg.res.window then
2064                 rset:addResource({
2065                         resource_name = "display"
2066                     })
2067                 rset.resources.display.attributes.pid = tostring(msg.pid)
2068                 rset.resources.display.attributes.appid = msg.appid
2069                 print("display name: " .. msg.res.display.name)
2070                 print("display zone:" .. msg.res.display.zone)
2071                 if msg.res.display.id then
2072                     print("display id: " .. msg.res.display.id)
2073                 end
2074             end
2075
2076             return rset
2077         end
2078
2079         -- parse the message
2080
2081         -- fields common to all messages:
2082         --      msg.command
2083         --      msg.appid
2084         --      msg.pid
2085
2086         if msg.command == 0x40011 then -- create_res
2087             print("command CREATE_RES")
2088
2089             if not sets.cid then
2090                 sets.cid = createResourceSet(self, cid, msg)
2091             end
2092
2093         elseif msg.command == 0x40012 then -- destroy_res
2094             print("command DESTROY_RES")
2095
2096             if sets.cid then
2097                 sets.cid:release()
2098             end
2099
2100             sets.cid = nil -- garbage collecting
2101
2102         elseif msg.command == 0x40001 then -- acquire_res
2103             print("command ACQUIRE_RES")
2104
2105             if not sets.cid then
2106                 sets.cid = createResourceSet(self, cid, msg)
2107             end
2108
2109             sets.cid:acquire()
2110
2111         elseif msg.command == 0x40002 then -- release_res
2112             print("command RELEASE_RES")
2113
2114             if sets.cid then
2115                 sets.cid:release()
2116             end
2117
2118         elseif msg.command == 0x40003 then -- deprive_res
2119             print("command DEPRIVE_RES")
2120
2121         elseif msg.command == 0x40004 then -- waiting_res
2122             print("command WAITING_RES")
2123
2124         elseif msg.command == 0x40005 then -- revert_res
2125             print("command REVERT_RES")
2126         end
2127     end
2128
2129     sc.inputdev_handler = function (self, cid, msg)
2130         if verbose > 0 then
2131             print('*** inputdev handler: ' .. command_name(msg.command))
2132             if verbose > 1 then
2133                 print(msg)
2134             end
2135         end
2136     end
2137
2138     sc.notify_handler = function (self, cid, msg)
2139         if verbose > 0 then
2140             print('### notify handler: ' .. command_name(msg.command))
2141             if verbose > 1 then
2142                 print(msg)
2143             end
2144         end
2145     end
2146 end
2147
2148 function send_driving_mode_to(client)
2149     if client == "" then
2150         return
2151     end
2152
2153     local driving_mode = mdb.select.select_driving_mode.single_value
2154
2155     if not driving_mode then driving_mode = 0 end
2156
2157     local reply = m:JSON({ command = 0x60001,
2158                            arg     = m:JSON({ stateid = 1,
2159                                               state   = driving_mode
2160                                      })
2161                   })
2162
2163     if verbose > 0 then
2164         print("### <== sending " .. command_name(reply.command) .. " message")
2165         if verbose > 1 then
2166             print(reply)
2167         end
2168     end
2169
2170     sc:send_message(client, reply)
2171 end
2172
2173 function send_night_mode_to(client)
2174     if client == "" then
2175         return
2176     end
2177
2178     local night_mode = mdb.select.select_night_mode.single_value
2179
2180     if not night_mode then night_mode = 0 end
2181
2182     local reply = m:JSON({ command = 0x60001,
2183                            arg     = m:JSON({ stateid = 2,
2184                                               state   = night_mode
2185                                      })
2186                   })
2187
2188     if verbose > 0 then
2189         print("### <== sending " .. command_name(reply.command) .. " message")
2190         if verbose > 1 then
2191             print(reply)
2192         end
2193      end
2194
2195      sc:send_message(client, reply)
2196 end
2197
2198 -- we should have 'audio_playback' defined by now
2199 m:try_load_plugin('telephony')